<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: mail.php 65 2013-09-21 11:59:34Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 65 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:59:34 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.php_mail.php');

if ( ($_GET['action'] == 'send_email_to_user') && ($_POST['customers_email_address']) && (!$_POST['back_x']) ) {
	switch ($_POST['customers_email_address']) {
		case '***':
			$mail = $db->db_query("SELECT 
										customers_firstname, 
										customers_lastname, 
										customers_email_address, 
										customers_gender 
									FROM 
										".TABLE_CUSTOMERS);
			$mail_sent_to = TEXT_ALL_CUSTOMERS;
			break;
		
		case '**D':
			$mail_query = $db->db_query("SELECT 
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_gender 
										FROM 
											".TABLE_CUSTOMERS." 
										WHERE 
											customers_newsletter = '1'");
			$mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
			break;
		
		default:
			if(is_numeric($_POST['customers_email_address'])) {
				$mail = $db->db_query("SELECT 
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_gender 
										FROM 
											".TABLE_CUSTOMERS." 
										WHERE 
											customers_status = ".$_POST['customers_email_address']);
												
				$sent_to = $db->db_query("SELECT 
												customers_status_name 
											FROM 
												".TABLE_CUSTOMERS_STATUS." 
											WHERE 
												customers_status_id = ".$db->db_prepare($_POST['customers_email_address'])." 
											AND 
												language_id='".$_SESSION['languages_id']."'");
				
				$mail_sent_to = $sent_to->fields['customers_status_name'];
			
			} else {
				
				$mail = $db->db_query("SELECT 
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_gender 
										FROM 
											".TABLE_CUSTOMERS." 
										WHERE 
											customers_email_address = ".$db->db_prepare($_POST['customers_email_address']));
				$mail_sent_to = $_POST['customers_email_address'];
			}
			break;
	}
	
	$smarty = new Smarty;
	require_once (DIR_FS_INC.'inc.get_mail_body.php');
	require_once (DIR_FS_INC.'inc.get_mail_data.php');
	$mail_data = get_mail_data('send_mail_from_admin');
	   
	$from = $_POST['from'];
    $subject = $_POST['subject'];

    while(!$mail->EOF) {
	
		$html_mail = '';
		$txt_mail = '';
		
		$smarty->assign('GENDER', $mail->fields['customers_gender']);
		$smarty->assign('NNAME', $mail->fields['customers_lastname']);
		$smarty->assign('CONTENT', stripslashes($_POST['emailcontent']));
		$smarty->assign('STORE_NAME', STORE_NAME);
		$smarty->assign('SEND_NAME', STORE_OWNER);
    	
		$html_mail = $smarty->fetch('html:send_mail_from_admin');
		$html_mail .= $signatur_html;
		$txt_mail = $smarty->fetch('txt:send_mail_from_admin');
		$txt_mail .= $signatur_text;
		
		php_mail($mail_data['EMAIL_ADDRESS'],
					$from,
					$mail->fields['customers_email_address'] ,
					$mail->fields['customers_firstname'].' '.$mail->fields['customers_lastname'],
					'',
					$mail_data['EMAIL_REPLAY_ADDRESS'],
					$mail_data['EMAIL_REPLAY_ADDRESS_NAME'],
					'',
					'',
					$subject,
					$html_mail,
					strip_tags($txt_mail));
		$mail->MoveNext();
    }
	
	redirect(href_link(FILENAME_MAIL, 'mail_sent_to='.urlencode($mail_sent_to)));
}
	
if(($_GET['action'] == 'preview') && (!$_POST['customers_email_address']))
	$message_stack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');

if ($_GET['mail_sent_to'])
	$message_stack->add(sprintf(NOTICE_EMAIL_SENT_TO, $_GET['mail_sent_to']), 'notice');

require(DIR_WS_INCLUDES.'metatag.php'); ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable">
		<tr>
 			<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
				<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
			</td>
	     	<td>
				<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
		    		<tr>
		     			<td class="pageHeading">
		        			Mail verschicken
		     			</td>
		   			</tr>
		   		</table>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<?php
				  if ( ($_GET['action'] == 'preview') && ($_POST['customers_email_address']) ) {
				    switch ($_POST['customers_email_address']) {
				      case '***':
				        $mail_sent_to = TEXT_ALL_CUSTOMERS;
				        break;

				      case '**D':
				        $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
				        break;

				      default:
				        if (is_numeric($_POST['customers_email_address'])) {
				          $sent_to = $db->db_query("SELECT 
		          										customers_status_name 
		          									FROM 
		          										".TABLE_CUSTOMERS_STATUS." 
		          									WHERE 
		          										customers_status_id = '".$_POST['customers_email_address']."' 
		          									AND 
		          										language_id='".$_SESSION['languages_id']."'");
				          $mail_sent_to = $sent_to->fields['customers_status_name'];
				          
				        } else
				          $mail_sent_to = $_POST['customers_email_address'];
				        
				        break;
				    }
				?>
          			<tr>
            			<td>
            				<?php echo draw_form('mail', FILENAME_MAIL, 'action=send_email_to_user'); ?>
							<table border="0" width="100%" cellpadding="0" cellspacing="2">
              					<tr>
                					<td>&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="smallText"><b><?php echo TEXT_CUSTOMER; ?></b><br /><?php echo $mail_sent_to; ?></td>
              					</tr>
              					<tr>
                					<td>&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="smallText"><b><?php echo TEXT_FROM; ?></b><br /><?php echo htmlspecialchars(stripslashes($_POST['from'])); ?></td>
              					</tr>
              					<tr>
                					<td>&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="smallText"><b><?php echo TEXT_SUBJECT; ?></b><br /><?php echo htmlspecialchars(stripslashes($_POST['subject'])); ?></td>
              					</tr>
              					<tr>
                					<td>&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="smallText"><b><?php echo TEXT_MESSAGE; ?></b><br /><?php echo stripslashes($_POST['emailcontent']); ?></td>
              					</tr>
              					<tr>
                					<td>&nbsp;</td>
              					</tr>
              					<tr>
                					<td><?php
									    reset($_POST);
									    while (list($key, $value) = each($_POST)) {
									      if (!is_array($_POST[$key])) {
									        echo draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
									      }
									    }
									?>
                						<table border="0" width="100%" cellpadding="0" cellspacing="2">
                  							<tr>
                    							<td><input type="submit" class="button" onClick="return confirm('<?php echo SAVE_ENTRY; ?>')" value="<?php echo BUTTON_BACK; ?>" name="back"></td>
                    							<td align="right"><?php echo '<a class="button" href="'.href_link(FILENAME_MAIL).'">'.BUTTON_CANCEL.'</a> <input type="submit" class="button" value="'.BUTTON_SEND_EMAIL.'">' ?></td>
                  							</tr>
                						</table>
									</td>
              					</tr>
            				</table>
            				</form>
						</td>
          			  </tr>
					<?php } else { ?>
          			<tr>
            			<td>
            			<?php echo draw_form('mail', FILENAME_MAIL, 'action=preview'); ?>
							<table border="0" cellpadding="0" cellspacing="2">
              					<tr>
                					<td colspan="2">&nbsp;</td>
              					</tr>
								<?php
								    $customers = array();
								    $customers[] = array('id' => '', 'text' => TEXT_SELECT_CUSTOMER);
								    $customers[] = array('id' => '***', 'text' => TEXT_ALL_CUSTOMERS);
								    $customers[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_CUSTOMERS);

								    $customers_statuses = $db->db_query("SELECT 
								    											customers_status_id, 
								    											customers_status_name 
								    										FROM 
								    											".TABLE_CUSTOMERS_STATUS." 
								    										WHERE 
								    											language_id='".$_SESSION['languages_id']."' 
								    										ORDER BY 
								    											customers_status_name");
								    while (!$customers_statuses->EOF) {
										$customers[] = array('id' => $customers_statuses->fields['customers_status_id'],
															'text' => $customers_statuses->fields['customers_status_name']);
										$customers_statuses->MoveNext();
								    }
								    
								    $customers_data = $db->db_query("SELECT 
								    								customers_email_address, 
								    								customers_firstname, 
								    								customers_lastname 
								    							FROM 
								    								".TABLE_CUSTOMERS." 
								    							ORDER BY 
								    								customers_lastname");
								    while(!$customers_data->EOF) {
										$customers[] = array('id' => $customers_data->fields['customers_email_address'],
															'text' => $customers_data->fields['customers_lastname'].', '.$customers_data->fields['customers_firstname'].' ('.$customers_data->fields['customers_email_address'].')');
										$customers_data->MoveNext();
								    }
								?>
              					<tr>
                					<td class="main"><?php echo TEXT_CUSTOMER; ?></td>
                					<td class="main"><?php echo draw_pull_down_menu('customers_email_address', $customers, $_GET['customer']);?></td>
              					</tr>
              					<tr>
                					<td colspan="2">&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="main"><?php echo TEXT_FROM; ?></td>
                					<td class="main"><?php echo draw_input_field('from', EMAIL_FROM); ?></td>
              					</tr>
              					<tr>
                					<td colspan="2">&nbsp;</td>
              					</tr>
              					<tr>
                					<td class="main"><?php echo TEXT_SUBJECT; ?></td>
                					<td class="main"><?php echo draw_input_field('subject'); ?></td>
              					</tr>
              					<tr>
                					<td colspan="2">&nbsp;</td>
              					</tr>
              					<tr>
                					<td valign="top"><?php echo TEXT_MESSAGE; ?></td>
                					<td><?php echo wysiwyg('emailcontent'); ?></td>
              					</tr>
              					<tr>
                					<td colspan="2">&nbsp;</td>
              					</tr>
              					<tr>
                					<td colspan="2" align="right"><input type="submit" class="button" value="<?php echo BUTTON_SEND_EMAIL; ?>"></td>
              					</tr>
            				</table>
            				 </form>
						</td>
          			  
					</tr>
					<?php } ?>
        		</table>
			</td>
      	</tr>
	</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>